Trigger Example 1: Simple Example
The Robotics_Trigger.project
sample project described here is located in the installation directory of CODESYS under ..\CODESYS SoftMotion\Examples
.
Triggers are used to find out exactly when an axis group reaches a specific point on the commanded path. This information can be used, for example, to switch a tool on or off at exactly the right time. This example shows how a single trigger is commanded on a movement and then read out cyclically. Based on this, the Trigger Example 2: Gluing Process project shows a realistic sample application.
Structure of the application
The application consists of two programs: a Main_PRG
running in the bus task and a Planning_PRG
running in the axis group planning task. In addition, there is also a trace.
Main_PRG
:
The Main_PRG
program contains the main state machine of the application. The first two states are used to initialize and switch on the drives and the axis group. In the STATE_COMMAND_MOVEMENT_WITH_TRIGGER
state, a trigger is first prepared using an instance from the SMC_GroupPrepareTrigger
function block. The position is specified relatively with a value of 0.6. As a result, the trigger is placed at the point where 60% of the assigned movement is completed. The prepared trigger is assigned to the next commanded movement. In this example, a linear movement to position (X=20, Y=10) is commanded by an MC_MoveLinearAbsolute
.
prepTrigger.PositionType := SMC_TriggerPositionType.MvtRelative; prepTrigger.Position.MvtPosition := 0.6; prepTrigger(AxisGroup:= AxisGroup, Execute:= TRUE); moveLinear(AxisGroup:= AxisGroup, Execute:= prepTrigger.Prepared);
In the next state STATE_ENABLE_READ_TRIGGER
, an instance of SMC_GroupReadTrigger
is started in order to read the state of the commanded trigger. The link to the trigger is established by means of the TriggerId
returned by SMC_GroupPrepareTrigger
.
readTrigger.TriggerId := prepTrigger.TriggerId; readTrigger.Enable := TRUE;
In the STATE_CHECK_TRIGGER
state, the status of the trigger is finally checked cyclically. As soon as SMC_GroupReadTrigger
reports Active
as the status, the duration until the trigger is reached can be read. The duration always refers to the start of the current cycle. Therefore, the trigger is reached in the current cycle if the duration is less than the cycle time (in this example, 0.004 s):
IF readTrigger.TriggerInfo.status = SMC_TRIGGER_STATUS.Active THEN IF readTrigger.TriggerInfo.triggerTime <= 0.004 THEN triggerFired := TRUE; ... END_IF END_IF
Planning_PRG
In the Planning_PRG
program, the forecast duration of the planning is configured by means of the SMC_TuneCPKernel
function block. The SMC_GroupReadTrigger
function block can output when a trigger is reached forecast duration. In the example, the forecast duration is set to 0.1 s. Therefore, the status of the SMC_GroupReadTrigger
function block changes to Active
0.1 s before the axis group reaches the trigger and outputs a valid duration from then on.
tuneCpKernel(AxisGroup:= axisGroup, Execute:= TRUE, fPlanningForecastDuration:= 0.1);
Trace
The trace can be used to track the behavior of the SMC_GroupReadTrigger
function block.
In the first diagram, you can see that a movement is executed from (X=0, Y=0) to (X=20, Y=10). The trigger is located at the relative position 0.6 on the movement, i.e. at (X=12, Y=6).
The second diagram shows the value of the triggerTime
output from SMC_GroupReadTrigger
. The value jumps to 0.1s because the forecast duration of the planning is 0.1s, and then drops linearly until the value 0 is reached at the position (X=12, Y=6).
The third diagram shows the value of the triggerFired
variable which is set in the STATE_CHECK_TRIGGER
state of Main_PRG
as soon as the axis group is less than 0.004 s away from the trigger.